# -*- coding: utf-8 -*-
# @File:     chat.py
# @Author:
# @DateTime: 2025/10/15/10:11

from app.db.base_class import Base, gen_uuid
from sqlalchemy import Boolean, Column, Integer, String, VARCHAR, BIGINT, SmallInteger, DateTime, ForeignKey, CheckConstraint, TEXT
from datetime import datetime
from sqlalchemy_serializer import SerializerMixin
from sqlalchemy.orm import relationship
from app.models.system import Users


class Dialog(Base, SerializerMixin):
    """
    对话表
    """
    __tablename__ = "chat_dialog"
    __table_args__ = ({'comment': '对话表'})

    dialog_uid = Column(VARCHAR(256), default=gen_uuid, index=True)
    user_id = Column(Integer, ForeignKey('system_users.id', ondelete='CASCADE'))
    user = relationship('Users', backref='dialog')  # , lazy='dynamic'
    agent_type = Column(VARCHAR(64), nullable=True, comment="对话Agent类型", default='Agent') # MCPAgent or Agent or RAGAgent


class DialogHistory(Base, SerializerMixin):
    """
    对话历史表
    """
    __tablename__ = "chat_dialog_history"
    __table_args__ = ({'comment': '对话历史表'})

    dialog_id = Column(Integer, ForeignKey('chat_dialog.id', ondelete='CASCADE'))
    dialog = relationship('Dialog', backref='dialog_history')  # , lazy='dynamic'
    role = Column(VARCHAR(64), comment="对话用户", default='assistant') # "assistant", "system", "user"
    content = Column(TEXT, comment="内容", nullable=True)


class RAGUploadFile(Base, SerializerMixin):
    """
    rag文件表
    """
    __tablename__ = "rag_upload_file"
    __table_args__ = ({'comment': 'rag文件表'})

    name = Column(VARCHAR(512), comment="名字")
    user_id = Column(Integer, ForeignKey('system_users.id', ondelete='CASCADE'))
    user = relationship('Users', backref='rag_file')  # , lazy='dynamic'
    path = Column(VARCHAR(1024), comment="文件")
    file_type = Column(VARCHAR(32), comment="文件类型", default='png', server_default='png')
    process_state = Column(Integer, default=1, comment="状态", server_default="1")    # 1.未处理  2.处理中  3.已完成 -1.失败
    vector_uid = Column(VARCHAR(256), comment="向量UID", nullable=True)



